package io.github.harmonly.skyblock.command;

import dev.jorel.commandapi.CommandAPI;
import dev.jorel.commandapi.CommandAPICommand;
import dev.jorel.commandapi.CommandPermission;
import dev.jorel.commandapi.arguments.ArgumentSuggestions;
import dev.jorel.commandapi.arguments.StringArgument;
import io.github.harmonly.skyblock.SkyBlock;
import io.github.harmonly.skyblock.util.Replacer;
import io.github.harmonly.skyblock.manager.ResourcesManager;
import io.github.harmonly.skyblock.util.StableObject;

public class LangCommand implements SBCommand {

    @Override
    public String name() {
        return "lang";
    }

    @Override
    public void register() {
        new CommandAPICommand("lang")
                .withPermission(CommandPermission.OP)
                .withArguments(new StringArgument("lang").replaceSuggestions(ArgumentSuggestions.strings(
                        "en_us", "zh_cn"
                )))
                .executes(((sender, args) -> {
                    String lang = args.getOrDefaultRaw(0, "en_us");

                    ResourcesManager.getConfig("config").set("lang", lang);

                    ResourcesManager.saveConfig("config");
                    ResourcesManager.setup(SkyBlock.getInstance());

                    StableObject json = ResourcesManager.getJson("message");
                    sender.sendMessage(Replacer.replaceIn(
                            json.getStringProperty("command.lang.success", "§cERROR"),
                            "lang", lang
                    ));
                }))
                .register();
    }

    @Override
    public void unregister() {
        CommandAPI.unregister("lang");
    }
}
